import { queryRule, removeRule, addRule,queryRunning,taskDo } from '../services/api';
import { message } from 'antd';

export default {
  namespace: 'rule',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    domainR:{
      id: 0,
      task_id: 0,
      name: "加载中",
      done_count: 0,
      total_count: 1,
    },
    clickR:{
      id: 0,
      task_id: 0,
      name: "加载中",
      done_count: 0,
      total_count: 1,
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const running = yield call(queryRunning)
      if (running.content.length == 2){
        yield put({
          type: 'saveRunning',
          payload: running.content,
        })
      }

      const response = yield call(queryRule, payload);      
      yield put({
        type: 'save',
        payload: response.content,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addRule, payload);
      const response2 = yield call(queryRule, {page:1,size:10});
      yield put({
        type: 'save',
        payload: response2.content,
      });
      if (callback) yield callback();
    },
    *taskdo({ payload, callback }, { call, put }) {
      const response = yield call(taskDo, payload);
      const response2 = yield call(queryRule, {page:1,size:10});
      yield put({
        type: 'save',
        payload: response2.content,
      });
      message.success('提交成功');
      if (callback) yield callback();
    },
    *delete({ payload, callback }, { call, put }) {
      const response = yield call(removeRule, payload);
      const response2 = yield call(queryRule, {page:1,size:10});
      yield put({
        type: 'save',
        payload: response2.content,
      });
      if (callback) yield callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveRunning(state, action) {
      return {
        ...state,
        domainR: action.payload[0],
        clickR: action.payload[1],
      };
    },
  },
};
